package com.livepass.android.girlspicks.activities;

import java.util.ArrayList;
import java.util.List;

import com.facebook.UiLifecycleHelper;
import com.facebook.widget.FacebookDialog;
import com.livepass.android.girlspicks.MyApplication;
import com.livepass.android.girlspicks.R;
import com.livepass.android.girlspicks.utils.Utilities;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Toast;

public class WebviewNoticeActivity extends Activity {

	private WebView notice;
	private UiLifecycleHelper uiHelper;
	
	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_webview_notice);
		notice = (WebView) findViewById(R.id.webview_notice);
		notice.getSettings().setJavaScriptEnabled(true);
		notice.loadUrl("file:///android_asset/webview_notice.html");
		
		// share facebook
		uiHelper = new UiLifecycleHelper(this, null);
		uiHelper.onCreate(savedInstanceState);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		uiHelper.onPause();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		uiHelper.onDestroy();
	}
	
	public void doBack(View v){
		setResult(RESULT_CANCELED);
		finish();
	}
	
	public void doShare(View view) {
		final String link = "http://girlspicks.co/mobile/notice.html";
		ArrayList<String> items;
		items = new ArrayList<String>();
		items.add("Facebookでシェア");
		items.add("Twitterでシェア");
		items.add("LINEでシェア");
		items.add("リンクをコピー");
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setItems(items.toArray(new CharSequence[items.size()]),
				new DialogInterface.OnClickListener() {
					@SuppressLint("NewApi")
					public void onClick(DialogInterface dialog, int which) {
						switch (which) {
						case 0:
							// Utilities.shareFBTapped(ArticleActivity.this,
							// "http://mp3.zing.vn/bai-hat/Weekend-Has-Come-2012-DJ/IW89E70F.html");
							if (Utilities.isPackageExisted(
									getApplicationContext(),
									"com.facebook.katana")) {
								FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(
										WebviewNoticeActivity.this).setLink(
										link)
										.setPicture("http://girlspicks.co/img/icon.png")
										.setCaption("Girls Picks")
										//.setDescription(ContentFragment.article.getOverview())
										//.setName(ContentFragment.article.getTitle())
										.build();
								uiHelper.trackPendingDialogCall(shareDialog
										.present());
							} else {
								// Uri uri =
								// Uri.parse("https://play.google.com/store/apps/details?id=com.facebook.katana");
								Uri uri = Uri
										.parse("https://www.facebook.com/sharer/sharer.php?u="
												+ link);
								Intent intent = new Intent(Intent.ACTION_VIEW,
										uri);
								startActivity(intent);
							}
							break;
						case 1:
							Intent intentTwitter = new Intent(
									Intent.ACTION_VIEW);
							intentTwitter.setData(Uri
									.parse("http://twitter.com/share?url="
											+ link
											+ "&count=horiztonal"));
							startActivity(intentTwitter);
							break;
						case 2:
							Intent intentLine;
							if (!Utilities.isPackageExisted(
									getApplicationContext(),
									"jp.naver.line.android")) {
								Uri uri = Uri
										.parse("https://play.google.com/store/apps/details?id=jp.naver.line.android");
								// Uri uri =
								// Uri.parse("https://www.facebook.com/sharer/sharer.php?u="
								// + ContentFragment.article.getOrgUrl());
								intentLine = new Intent(Intent.ACTION_VIEW, uri);
								startActivity(intentLine);
							} else {
								intentLine = new Intent(Intent.ACTION_SEND);
								intentLine.setType("text/plain");
								intentLine.putExtra(Intent.EXTRA_TEXT, link);

								PackageManager pm = getPackageManager();
								Intent sendIntent = new Intent(
										Intent.ACTION_SEND);
								sendIntent.setType("text/plain");
								List<ResolveInfo> resInfo = pm
										.queryIntentActivities(sendIntent, 0);
								for (int i = 0; i < resInfo.size(); i++) {
									ResolveInfo ri = resInfo.get(i);
									String packageName = ri.activityInfo.packageName;
									if (packageName
											.contains("jp.naver.line.android")) {

										intentLine.setClassName(
												ri.activityInfo.packageName,
												ri.activityInfo.name);
										startActivity(intentLine);
									}
								}

							}
							break;
						case 3:
							Toast.makeText(getApplicationContext(),
									"Copied link", Toast.LENGTH_SHORT).show();
							int sdk = android.os.Build.VERSION.SDK_INT;
							if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
								android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
								clipboard.setText(link);
							} else {
								android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
								android.content.ClipData clip = android.content.ClipData
										.newPlainText("Copied Text", link);
								clipboard.setPrimaryClip(clip);
							}
							break;
						default:
							break;
						}
					}
				});
		builder.create().show();
	}

}
